<?
if (!empty($_GET['Comments_id'])) {
	$sql = "SELECT * FROM er_comments WHERE Comments_id = ".$_GET['Comments_id'];
	$result = r_db($sql);
	$row = mysql_fetch_array($result);
	$comment = $row['comment'];
	$rank = $row['rank'];
}
else {
	$comment = '';
	$rank = '';
}
?>
<SCRIPT language="javascript">
function verify() {
	var themessage = "You are required to complete the following fields: \n";
	if (document.form.txtComments.value=="") {
		themessage = themessage + " - Comments\n";
	}
	if (themessage == "You are required to complete the following fields: \n") {
		//alert(themessage);
		document.form.submit();
	}
	else {
		alert(themessage);
		return false;
	}
}

function remove_XS_whitespace(item)
{
	item.value = item.value.replace(/\r/g, " ");
	item.value = item.value.replace(/[^ A-Za-z0-9`~!@#\$%\^&\*\(\)-_=\+\\\|\]\[\}\{'";:\?\/\.>,<]/g, "");
	item.value = item.value.replace(/'/g, "");
	item.value = item.value.replace(/ +/g, " ");
	item.value = item.value.replace(/^\s/g, "");
	item.value = item.value.replace(/\s$/g, "");
	if (item.value == ' '){item.value = ''};
}

function setPresetComment(preset) {
	comment = document.forms['form'].elements['txtComments'].value;
	if (comment == '') {
		document.forms['form'].elements['txtComments'].value=preset;
	}
	else {
		document.forms['form'].elements['txtComments'].value=document.forms['form'].elements['txtComments'].value+'\n'+preset;
	}
}
</SCRIPT>

<h1>Comment Management</h1>
<form name=form method="POST" action="?page=submit_comment_add">
All required fields are marked with by the <img src="images/required.gif" alt="required field" /> icon.
<hr>
<TABLE width="80%">
	<tr>
	<? if (!empty($_GET['Comments_id']))
		echo "<td colspan=\"2\"><h3>Enter details below to modify the selected comment.</h3></td>";
	else
		echo "<td colspan=\"2\"><h3>Enter details below to create a comment.</h3></td>";
	?>
	</tr>
	<tr>
		<td>Pre-set Comment</td>
		<td>
			<select onchange="setPresetComment(this.value)">
				<option value=""></option>
				<option value="* A-Z">* A-Z</option>
				<option value="Client Contact form completed">Client Contact form completed</option>
			</select>
	</tr>
	<TR>
		<td valign="top">Comments: <img src="images/required.gif" alt="required field" /></td>
		<td><textarea rows="5" name="txtComments" cols="40"><? echo $comment; ?></textarea></td>
	</tr>
	<tr>
		<td>Rank</td>
		<td><select name="cmbRank">
			<OPTION <? if ($rank == "Noteworthy" || empty($rank)) echo "SELECTED"; ?>>Noteworthy</OPTION>
			<OPTION <? if ($rank == "Important") echo "SELECTED"; ?>>Important</OPTION>
			<OPTION <? if ($rank == "Very Important") echo "SELECTED"; ?>>Very Important</OPTION>
		</select></td>
		<input type="hidden" name="ClientID" value="<? echo $_GET['ClientID']; ?>">
		<input type="hidden" name="Comments_id" value="<? if (isset($_GET['Comments_id'])) echo $_GET['Comments_id']; ?>">
		<input type="hidden" name="Delete" value="0">
	</tr>
	<tr>
		<td width="50%"><input type="reset" value="Reset"> <input type="button" value="Delete" onclick="if (Comments_id.value == '') alert('Comment must be saved and selected before it can be deleted.');  if (Comments_id.value > 0 && confirm('Are you sure you want to delete the current comment?')) {Delete.value=1; submit();}"></td>
		<td width="50%"><input type="button" value="Save" onclick="verify();"></td>
	</tr>
</table>
</form>
<table width="80%">
<tr style="font-weight: bold"><td width="20%">&nbsp;</td><td width="20%">Date</td><td width="40%">Comment</td><td width="20%">Rank</td></tr>
<?
	$sql = "SELECT * FROM `er_comments` where client_id = ".$_GET['ClientID']." ORDER BY comment_datetime";
	$result = r_db($sql);
	while ($row = mysql_fetch_array($result)) {
		$commentDate = $row['comment_datetime'];
		$commentDateAmerican=explode("-",substr($commentDate,0,10));
		$day=$commentDateAmerican[0];
		$month=$commentDateAmerican[1];
		$year=$commentDateAmerican[2];
		$commentDateEnglish=$year."/".$month."/".$day;
		echo  "<tr><td width=\"20%\" valign=\"top\"><a href=\"?page=comment_add&ClientID=".$_GET['ClientID']."&Comments_id=".$row['Comments_id']."\">Modify</a></td><td valign=\"top\">$commentDateEnglish</td><td valign=\"top\">".$row['comment']."</td><td valign=\"top\">".$row['rank']."</td></tr>";
	}
	echo  "<tr><td width=\"20%\" valign=\"top\"><a href=\"?page=comment_add&ClientID=".$_GET['ClientID']."\">New Comment</a></td></tr>";
?>
</table>